package com.neusoft.neuiotms.dcom.converter;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Joiner;
import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.neuiotms.dcom.constants.DCOMConstants;
import com.neusoft.neuiotms.dcom.dto.CMDBInstanceDTO;
import com.neusoft.neuiotms.dcom.dto.MaintainPlanDTO;
import com.neusoft.neuiotms.dcom.model.MaintainPlan;
import com.neusoft.neuiotms.dcom.model.MaintainRule;
import com.neusoft.neuiotms.dcom.repository.MaintainPlanRepository;
import com.neusoft.neuiotms.dcom.repository.MaintainRuleRepository;

@Component
public class MaintainPlanConverter extends AbstractConverter<MaintainPlan, MaintainPlanDTO> {

    @Autowired
    private MaintainPlanRepository maintainPlanRepository;
    @Autowired
    private MaintainRuleRepository maintainRuleRepository;
    @Autowired
    private BaseIdGenerator maintainPlanCodeGenerator;

    @Override
    public MaintainPlan toModel(final MaintainPlanDTO dto) {
        MaintainPlan model;
        if (dto.isNew()) {
            model = new MaintainPlan();
            model.setCreateTime(new Date());
        } else {
            model = this.maintainPlanRepository.getOne(dto.getId());
            model.setUpdateTime(new Date());
        }
        model.setCode(
                StringUtils.isEmpty(dto.getCode()) ? this.maintainPlanCodeGenerator.generateCode() : dto.getCode());

        model.setStatus(dto.getStatus());

        model.setChargeUserId(dto.getChargeUserId());
        model.setChargeUserCode(dto.getChargeUserCode());
        model.setChargeUserName(dto.getChargeUserName());

        model.setJsonObjs(dto.getJsonObjs());

        if (StringUtils.isNotBlank(dto.getRuleCode())) {
            final MaintainRule rule = this.maintainRuleRepository.getByCode(dto.getRuleCode());
            model.setRule(rule);
        }
        model.setName(dto.getName());
        model.setOrgCode(dto.getOrgCode());
        model.setOrgName(dto.getOrgName());
        model.setPeriod(dto.getPeriod());
        model.setUserId(dto.getUserId());
        model.setUserName(dto.getUserName());
        model.setVersion(dto.getVersion());
        model.setDeleteFlag(dto.isDeleteFlag());

        return model;
    }

    @Override
    public MaintainPlanDTO toDTO(final MaintainPlan model, final boolean forListView) {
        final MaintainPlanDTO dto = new MaintainPlanDTO();

        dto.setId(model.getId());

        dto.setChargeUserId(model.getChargeUserId());
        dto.setChargeUserCode(model.getChargeUserCode());
        dto.setChargeUserName(model.getChargeUserName());

        dto.setCode(model.getCode());

        dto.setJsonObjs(model.getJsonObjs());

        if (StringUtils.isNotEmpty(model.getJsonObjs())) {
            final List<CMDBInstanceDTO> iList = JSON.parseArray(model.getJsonObjs(), CMDBInstanceDTO.class);
            if (CollectionUtils.isNotEmpty(iList)) {
                final List<String> nameList =
                        iList.stream().map(CMDBInstanceDTO::getName).collect(Collectors.toList());
                dto.setInstancesName(Joiner.on(DCOMConstants.COMMA_SPILT).join(nameList));
            }

            if ((model.getRule() == null) && CollectionUtils.isNotEmpty(iList)
                    && (iList.get(0).getSchemadef() != null)) {
                dto.setModelCode(iList.get(0).getSchemadef().getCode());
                dto.setModelName(iList.get(0).getSchemadef().getName());
            }
        }

        if (model.getRule() != null) {
            dto.setRuleId(model.getRule().getId());
            dto.setRuleCode(model.getRule().getCode());
            dto.setRuleName(model.getRule().getName());
            dto.setModelCode(model.getRule().getModelCode());
            dto.setModelName(model.getRule().getModelName());
        }

        dto.setName(model.getName());
        dto.setOrgCode(model.getOrgCode());
        dto.setOrgName(model.getOrgName());
        dto.setPeriod(model.getPeriod());
        dto.setStatus(model.getStatus());
        dto.setUserId(model.getUserId());
        dto.setUserName(model.getUserName());
        dto.setVersion(model.getVersion());
        dto.setDeleteFlag(model.isDeleteFlag());

        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());
        return dto;
    }

}
